package com.wz.wzliving.commodity.service.impl;

import com.wz.wzliving.commodity.vo.Catalog2Vo;
import com.wzcommon.utils.PageUtils;
import com.wzcommon.utils.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.wz.wzliving.commodity.dao.CategoryDao;
import com.wz.wzliving.commodity.entity.CategoryEntity;
import com.wz.wzliving.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public List<CategoryEntity> listTree() {
        //1、查出所有分类数据
        List<CategoryEntity> entities = baseMapper.selectList(null);
//2、组装成层级树形结构
        List<CategoryEntity> categoryTree = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentId() == 0 //过滤，返回 1 级分类
        ).map((category) -> {
//进行 map 映射操作, 给每一个一级分类设置对应的子分类,
//这个 getChildrenCategories 是递归操作
            category.setChildrenCategories(getChildrenCategories(category, entities));
            return category;
        }).sorted((category1, category2) -> { //排序
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList()); //返回集合

        return categoryTree;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Long[] getCascadedCategoryId(Long categoryId) {
        List<Long> cascadedCategoryId = new ArrayList<>();
        getParentCategoryId(categoryId, cascadedCategoryId);

//将顺序翻转
        Collections.reverse(cascadedCategoryId);
        return cascadedCategoryId.toArray(new Long[cascadedCategoryId.size()]);
    }

    @Override
    public List<CategoryEntity> getLevel1Categorys() {
        QueryWrapper<CategoryEntity> categoryEntityQueryWrapper = new QueryWrapper<>();
        categoryEntityQueryWrapper.eq("parent_id", 0);
        List<CategoryEntity> categoryEntities = this.baseMapper.selectList(categoryEntityQueryWrapper);
        return categoryEntities;
    }

    @Override
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        //将数据库的多次查询变为一次
        List<CategoryEntity> selectList = this.baseMapper.selectList(null);
        //1、查出所有分类
        //1、1）查出所有一级分类
        List<CategoryEntity> level1Categorys = getParent_cid(selectList, 0L);
//封装数据
        Map<String, List<Catalog2Vo>> parentCid =
                level1Categorys.stream().collect(Collectors.toMap(k -> k.getId().toString(), v ->
                {
                    //1、每一个的一级分类,查到这个一级分类的二级分类
                    List<CategoryEntity> categoryEntities = getParent_cid(selectList, v.getId());
                    //2、封装上面的结果

                    List<Catalog2Vo> catalog2Vos = null;
                    if (categoryEntities != null) {
                        catalog2Vos = categoryEntities.stream().map(l2 -> {
                            Catalog2Vo catalog2Vo =
                                    new Catalog2Vo(v.getId().toString(), null, l2.getId().toString(), l2.getName().toString());
                            //1、找当前二级分类的三级分类封装成 vo
                            List<CategoryEntity> level3Catelog = getParent_cid(selectList,
                                    l2.getId());
                            if (level3Catelog != null) {
                                List<Catalog2Vo.Category3Vo> category3Vos =
                                        level3Catelog.stream().map(l3 -> {
                                            //2、封装成指定格式
                                            Catalog2Vo.Category3Vo category3Vo =
                                                    new Catalog2Vo.Category3Vo(l2.getId().toString(),
                                                            l3.getId().toString(), l3.getName());
                                            return category3Vo;
                                        }).collect(Collectors.toList());

                                catalog2Vo.setCatalog3List(category3Vos);
                            }
                            return catalog2Vo;
                        }).collect(Collectors.toList());
                    }
                    return catalog2Vos;
                }));
        return parentCid;
    }

    private List<CategoryEntity>
    getParent_cid(List<CategoryEntity> selectList, Long parentCid) {
        List<CategoryEntity> categoryEntities = selectList.stream().filter(item ->
                item.getParentId().equals(parentCid)).collect(Collectors.toList());
        return categoryEntities;
// return this.baseMapper.selectList(
// new QueryWrapper<CategoryEntity>().eq("parent_cid", parentCid));
    }

    private List<Long> getParentCategoryId(Long categoryId, List<Long> categories) {
        categories.add(categoryId);
        CategoryEntity categoryEntity = this.getById(categoryId);
        if (categoryEntity.getParentId() != 0) {
            getParentCategoryId(categoryEntity.getParentId(), categories);
        }
        return categories;
    }

    private List<CategoryEntity> getChildrenCategories
            (CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().longValue() == root.getId().longValue();
        }).map(categoryEntity -> {
//1、找到子菜单, 并设置
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all));
            return categoryEntity;
        }).sorted((category1, category2) -> {
//2、菜单的排序
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList());
        return children;

    }
}